<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">

                    <?php if (!in_array('img', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['img']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="img" id="img_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="image_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('img1', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['img1']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="img1" id="img1_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="image1_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('video', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['video']?></label>
                        <div class="layui-input-block">
                            <video src="" autoplay="true" controls="true" height="100"></video>
                            <input type="text" style="display:none" name="video" id="video_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="video_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传视频
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('active_img', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['active_img']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="active_img" id="active_img_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="active_image_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>


                    <?php if (!in_array('title', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['title']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('name', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['name']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>



                    <?php if (!in_array('img_url', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['img_url']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="img_url" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('desc', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['desc']?></label>
                        <div class="layui-input-block">
                            <textarea name="desc" id="desc" cols="30" class="layui-textarea" rows="10"></textarea>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('en_name', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['en_name']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="en_name" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('en_title', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['en_title']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="en_title" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('en_desc', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['en_desc']?></label>
                        <div class="layui-input-block">
                            <textarea name="en_desc" id="en_desc" cols="30" class="layui-textarea" rows="10"></textarea>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('icon', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['icon']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="icon" id="icon_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="icon_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('content', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['content']?></label>
                        <div class="layui-input-block">
                            <textarea name="content" id="content" cols="30" rows="10"></textarea>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('en_content', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['en_content']?></label>
                        <div class="layui-input-block">
                            <textarea name="en_content" id="en_content" cols="30" rows="10"></textarea>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('active_icon', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['active_icon']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="active_icon" id="active_icon_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="active_icon_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['sort']?></label>
                        <div class="layui-input-block">
                            <input type="number" name="sort" value="" class="layui-input">
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <input type="hidden" name="position_id" value="<?=$position_id?>" class="layui-input">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            // 相关接口
            const INSERT_API = "/app/admin/page-content/insert";

            layui.use(['tinymce'], function() {
                var tinymce = layui.tinymce
                editor = tinymce.render({
                    elem: "#content",
                    images_upload_url: "/app/admin/upload/image",
                    height: 400
                });

                editor2 = tinymce.render({
                    elem: "#en_content",
                    images_upload_url: "/app/admin/upload/image",
                    height: 400
                });

            });

            // 图片上传逻辑
            let $ = layui.jquery;

            // Image upload for "图片"
            let imgInput = $("#image_upload").prev();
            imgInput.prev().attr("src", imgInput.val());
            layui.upload.render({
                elem: "#image_upload",
                url: "/app/admin/upload/image",
                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                done: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                }
            });

            // Image upload for "动态选中后的图片"
            let activeImgInput = $("#active_image_upload").prev();
            activeImgInput.prev().attr("src", activeImgInput.val());
            layui.upload.render({
                elem: "#active_image_upload",
                url: "/app/admin/upload/image",
                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                done: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                }
            });

            let iconInput = $("#icon_upload").prev();
            iconInput.prev().attr("src", iconInput.val());
            layui.upload.render({
                elem: "#icon_upload",
                url: "/app/admin/upload/image",
                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                done: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                }
            });

            let activeIconInput = $("#active_icon_upload").prev();
            activeIconInput.prev().attr("src", activeIconInput.val());
            layui.upload.render({
                elem: "#active_icon_upload",
                url: "/app/admin/upload/image",
                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                done: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                }
            });

            let img1Input = $("#image1_upload").prev();
            img1Input.prev().attr("src", img1Input.val());
            layui.upload.render({
                elem: "#image1_upload",
                url: "/app/admin/upload/image",
                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                done: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                }
            });

            let videoInput = $("#video_upload").prev();
            videoInput.prev().attr("src", videoInput.val());
            layui.upload.render({
                elem: "#video_upload",
                url: "/app/admin/upload/file",
                acceptMime: "video/mp4",
                accept: "file",
                done: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                }
            });
            
            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>
</html>
